package com.qdu.controller.BackstageViewController.entry;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.qdu.controller.BackstageViewController.BaseController;
import com.qdu.entity.sys.SysAccount;
import com.qdu.service.sys.AccountService;
import com.qdu.vo.ResultVO;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @author:sanShi
 * @Description:{登陆注册业务处理}
 * @date :2020/12/1
 */
@RestController
@RequestMapping("/api/entry")
@RequiredArgsConstructor
public class LoginApiController extends BaseController {
    private final AccountService sysAccountService;

    @GetMapping("/login")
//    @AddLoginLog(logLoginCategory = LogLoginCategoryEnum.PASSWORD_LOGIN)
    public ResultVO<?> login(HttpServletRequest request,
                             String username,
                             String password) {
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password)) {
            return ResultVO.failure("登录帐号和密码不能为空");
        }
        val sysAccount = new SysAccount();
        sysAccount.setCode(username).setPassword(SecureUtil.md5(password));
        val user = sysAccountService.login(sysAccount);
        if (user != null) {
            if (user.getIsLocked()) {
                return ResultVO.failure("该帐号已经被锁定,无法进行登录");
            }
            //用户的资源存session
//            setUserResource(request.getSession(), user);
//            setLoginCount(user);
            return ResultVO.success("登录成功", "/back_index");
        } else {
            return ResultVO.failure("账户或密码错误!");
        }
    }

    /**
     * 设置登录权限
     *
     * @param session    session
     * @param sysAccount 用户
     */
//    private void setUserResource(HttpSession session, SysAccount sysAccount) {
//        session.setAttribute(Constants.USER_SESSION, sysAccount);
//        session.setMaxInactiveInterval(Constants.SESSION_TIME_OUT);
//        // 用户角色
//        val sysRolesAccountList = sysRolesAccountService.findByAccountId(sysAccount.getId());
//        List<Long> roleIdList = new ArrayList<>();
//        for (SysRolesAccount sysRolesAccount : sysRolesAccountList) {
//            roleIdList.add(sysRolesAccount.getRoleId());
//        }
//        if (roleIdList.size() == 0) {
//            return;
//        }
//        // 角色权限
//        val sysPermissionList = sysPermissionService.findByRoleIdList(roleIdList);
//        List<Long> idList = new ArrayList<>();
//        for (SysPermission sysPermission : sysPermissionList) {
//            idList.add(sysPermission.getSubmenuId());
//        }
//        if (idList.size() == 0) {
//            return;
//        }
//        // 权限
//        val sysResourceList = sysResourceService.findByIdList(idList);
//        session.setAttribute(Constants.USER_RESOURCE_KEY, sysResourceList);
//    }

    /**
     * 设置登录次数和时间
     *
     * @param sysAccount 用户
     */
//    private void setLoginCount(SysAccount sysAccount) {
//        sysAccount.setLoginCount(sysAccount.getLoginCount() != null ? sysAccount.getLoginCount() + 1 : 1).setLastLoginTime(new Date());
//        sysAccountService.updateById(sysAccount);
//    }
}
